package com.harry.ribbonconsumer;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author Harry
 * @date 2018/3/28 14:33
 */
@Service
public class HelloService {

    private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "helloFallback",commandKey = "helloKey")
    public String hello(){

        long start = System.currentTimeMillis();

        StringBuilder result = new StringBuilder();

        result.append(restTemplate.getForEntity("http://hello-server/hello",String.class).getBody());

       long end = System.currentTimeMillis();

        logger.info("Spend time : " + (end - start) );

        return result.toString();
    }

	public String helloFallback() {
		return "error";
	}

}
